package service

import (
	"context"
	"gitee.com/cristiane/micro-mall-search/model/elasticsearch"
	"gitee.com/cristiane/micro-mall-search/pkg/code"
	"gitee.com/cristiane/micro-mall-search/proto/micro_mall_search_proto/search_business"
	"gitee.com/cristiane/micro-mall-search/repository"
	"strconv"
)

func SkuInventorySearch(ctx context.Context, query string) (result []*search_business.SkuInventoryEntry, retCode int) {
	result = make([]*search_business.SkuInventoryEntry, 0)
	retCode = code.Success
	match := map[string]interface{}{
		"name": query,
	}
	rsp, searchCode := repository.Search(ctx, elasticsearch.ESIndexSkuInventory, match, 10, 0)
	if searchCode != code.Success {
		retCode = code.ErrorServer
		return
	}
	result = make([]*search_business.SkuInventoryEntry, rsp.Total.Value)
	for i := range rsp.Hits {
		shopId, _ := rsp.Hits[i].Source["shop_id"].(int64)
		skuName, _ := rsp.Hits[i].Source["name"].(string)
		entry := &search_business.SkuInventoryEntry{
			SkuCode: rsp.Hits[i].ID,
			ShopId:  strconv.FormatInt(shopId, 10),
			SkuName: skuName,
			Score:   rsp.Hits[i].Score,
		}
		result[i] = entry
	}

	return result, retCode
}

func ShopSearch(ctx context.Context, query string) (result []*search_business.ShopSearchEntry, retCode int) {
	result = make([]*search_business.ShopSearchEntry, 0)
	retCode = code.Success
	match := map[string]interface{}{
		"nick_name": query,
	}
	rsp, searchCode := repository.Search(ctx, elasticsearch.ESIndexShop, match, 10, 0)
	if searchCode != code.Success {
		retCode = code.ErrorServer
		return
	}
	result = make([]*search_business.ShopSearchEntry, rsp.Total.Value)
	for i := range rsp.Hits {
		shopName, _ := rsp.Hits[i].Source["nick_name"].(string)
		entry := &search_business.ShopSearchEntry{
			ShopId:   rsp.Hits[i].ID,
			ShopName: shopName,
			Score:    rsp.Hits[i].Score,
		}
		result[i] = entry
	}

	return result, retCode
}

func UserSearch(ctx context.Context, query string) (result []*search_business.UserInfoEntry, retCode int) {
	result = make([]*search_business.UserInfoEntry, 0)
	retCode = code.Success
	match := map[string]interface{}{
		"user_name": query,
	}
	rsp, searchCode := repository.Search(ctx, elasticsearch.ESIndexUserInfo, match, 10, 0)
	if searchCode != code.Success {
		retCode = code.ErrorServer
		return
	}
	result = make([]*search_business.UserInfoEntry, rsp.Total.Value)
	for i := range rsp.Hits {
		userName, _ := rsp.Hits[i].Source["user_name"].(string)
		entry := &search_business.UserInfoEntry{
			Name:  userName,
			Phone: rsp.Hits[i].ID,
			Score: rsp.Hits[i].Score,
		}
		result[i] = entry
	}
	return result, retCode
}

func MerchantSearch(ctx context.Context, query string) (result []*search_business.MerchantInfoEntry, retCode int) {
	result = make([]*search_business.MerchantInfoEntry, 0)
	retCode = code.Success
	match := map[string]interface{}{
		"register_addr": query,
	}
	rsp, searchCode := repository.Search(ctx, elasticsearch.ESIndexMerchantsMaterialInfo, match, 10, 0)
	if searchCode != code.Success {
		retCode = code.ErrorServer
		return
	}
	result = make([]*search_business.MerchantInfoEntry, rsp.Total.Value)
	for i := range rsp.Hits {
		uid, _ := rsp.Hits[i].Source["uid"].(int64)
		userName, _ := rsp.Hits[i].Source["register_addr"].(string)
		entry := &search_business.MerchantInfoEntry{
			Uid:          uid,
			UserName:     userName,
			MerchantCode: rsp.Hits[i].ID,
			Score:        rsp.Hits[i].Score,
		}
		result[i] = entry
	}
	return result, retCode
}

func TradeOrderSearch(ctx context.Context, query string) (result []*search_business.TradeOrderInfo, retCode int) {
	result = make([]*search_business.TradeOrderInfo, 0)
	retCode = code.Success
	match := map[string]interface{}{
		"goods_name": query,
	}
	rsp, searchCode := repository.Search(ctx, elasticsearch.ESIndexTradeOrder, match, 10, 0)
	if searchCode != code.Success {
		retCode = code.ErrorServer
		return
	}
	result = make([]*search_business.TradeOrderInfo, rsp.Total.Value)
	for i := range rsp.Hits {
		entry := &search_business.TradeOrderInfo{
			OrderCode: rsp.Hits[i].ID,
			Score:     rsp.Hits[i].Score,
		}
		result[i] = entry
	}

	return result, retCode
}
